package CompletableFuturefd.bachCF;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BatchProcessingExample {

    private static final ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        System.out.println("=======开始=====");
        List<Integer> items = getItems(); // 假设这是一个获取待处理数据的方法
        // 分批处理
        List<List<Integer>> batches = Lists.partition(items, 10);
        batches.forEach(batch -> processBatch(batch, executor));
        System.out.println("======结束====");
        // 关闭executor服务，防止资源泄露
        executor.shutdown();
    }



    private static void processBatch(List<Integer> batch, ExecutorService executor) {
        CompletableFuture.runAsync(() -> {
            // 处理每个批次的数据
            batch.forEach(item -> {
                // 实际的处理逻辑
                System.out.println("Processing item: " + item);
            });
        }, executor);

    }
    private static List<Integer> getItems() {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <=100; i++) {
            list.add(i);
        }
        return list;
    }
}